package com.handinglian.system.controller;

import com.apidoc.annotation.Api;
import com.apidoc.annotation.ApiVersion;
import com.handinglian.common.dto.ResultData;
import com.handinglian.common.factory.ResultDataFactory;
import com.handinglian.system.dto.MenuDto;
import com.handinglian.system.dto.RoleDto;
import com.handinglian.system.param.RoleCreateParam;
import com.handinglian.system.param.RoleUpdateParam;
import com.handinglian.system.service.MenuService;
import com.handinglian.system.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("角色权限")
@RestController
@Slf4j
@RequestMapping("/{version}/role")
@ApiVersion(1.0)
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    /**
     * 创建角色
     */
    @RequiresPermissions("role_manage:create")
    @PostMapping("/createRole")
    public ResultData createRole(@RequestBody RoleCreateParam roleCreateParam){
        int amount = roleService.createRole(roleCreateParam);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 删除角色
     */
    @RequiresPermissions("role_manage:delete")
    @DeleteMapping(value = "/deleteRole")
    public ResultData deleteRole(Integer roleId) {
        int amount = roleService.deleteRole(roleId);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 更新角色
     */
    @RequiresPermissions("role_manage:update")
    @PutMapping(value = "/updateRole")
    public ResultData updateRole(@RequestBody RoleUpdateParam roleUpdateParam) {
        int amount = roleService.updateRole(roleUpdateParam);
        return ResultDataFactory.generateResultData(amount);
    }


    /**
     * 查询角色
     */
    @RequiresPermissions("role_manage:list")
    @GetMapping(value = "/loadRole")
    public ResultData<RoleDto> loadRole(Integer roleId) {
        RoleDto role = roleService.loadRole(roleId);
        return ResultDataFactory.generateSuccessResultData(role);
    }

    /**
     * 获取角色列表
     */
    @RequiresPermissions("role_manage:list")
    @GetMapping("/inquireRoleList")
    public ResultData<RoleDto> inquireRolePageList() {
        List<RoleDto> roleList = roleService.inquireRoleList();
        return ResultDataFactory.generateSuccessResultData(roleList);
    }

    /**
     * 获取菜单权限列表
     */
    @RequiresPermissions("role_manage:list")
    @GetMapping("/inquireMenuPermissionList")
    public ResultData<MenuDto> inquireMenuPermissionList() {
        List<MenuDto> menuDtos = menuService.inquireMenuPermissionList();
        return ResultDataFactory.generateSuccessResultData(menuDtos);
    }

    /**
     * 获取角色权限
     */
    @RequiresPermissions("role_manage:list")
    @GetMapping("/inquireRolePermissionList")
    public ResultData<RoleDto> inquireRolePermissionList(Integer roleId) {
        RoleDto roleDto = roleService.inquireRolePermissionList(roleId);
        return ResultDataFactory.generateSuccessResultData(roleDto);
    }
}
